
package com.yuan.unit;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;

/**
 * Preference工具类
 * 
 * @author YUAN
 */
public class PreferenceUtil {
    private final static PreferenceUtil sInstance = new PreferenceUtil();
    private SharedPreferences mPref;
    private final static String PREFERENCE_NAME = "yuanwei_graduate";

    private PreferenceUtil() {
    }

    public void init(Application context) {
        mPref = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
    }

    public static PreferenceUtil getInstance() {
        return sInstance;
    }

    public SharedPreferences pref() {
        return mPref;
    }

    public SharedPreferences.Editor editor() {
        return mPref.edit();
    }

    public int getInt(String key, int defValue) {
        return mPref.getInt(key, defValue);
    }

    public PreferenceUtil putInt(String key, int value) {
        mPref.edit().putInt(key, value).commit();
        return this;
    }

    public String getString(String key, String defValue) {
        return mPref.getString(key, defValue);
    }

    public PreferenceUtil putString(String key, String value) {
        if (key != null && value != null)
        {
            mPref.edit().putString(key, value).commit();
        }
        return this;
    }

    public boolean getBoolean(String key, boolean defValue) {
        if (key != null)
        {
            return mPref.getBoolean(key, defValue);
        }
        return false;
    }

    public PreferenceUtil putFloat(String key, float value) {
        mPref.edit().putFloat(key, value).commit();
        return this;
    }

    public float getFloat(String key, float defValue) {
        return mPref.getFloat(key, defValue);
    }

    public PreferenceUtil putBoolean(String key, boolean value) {
        mPref.edit().putBoolean(key, value).commit();
        return this;
    }

    public boolean contains(String key) {
        return mPref.contains(key);
    }

}
